#include "CMyFileSystem.h"

CMyFileSystem::CMyFileSystem(QObject* parent)
    : QObject(parent)
{
    // 增加信号槽监听
    connect(&m_fileWatcher, SIGNAL(directoryChanged(const QString&)), this, SLOT(slotDirectoryChanged(const QString&)));
    connect(&m_fileWatcher, SIGNAL(fileChanged(const QString&)), this, SLOT(slotFileChanged(const QString&)));

    // 定时器监听
    m_directorTimer.setSingleShot(true);
    m_fileTimer.setSingleShot(true);
    connect(&m_directorTimer, SIGNAL(timeout()), this, SLOT(slotDirectorTimerOut()));
    connect(&m_fileTimer, SIGNAL(timeout()), this, SLOT(slotFileTimerOut()));
}

CMyFileSystem::~CMyFileSystem()
{
}

bool CMyFileSystem::addWatcherPath(const QString& path)
{
    return m_fileWatcher.addPath(path);
}

QStringList CMyFileSystem::addWatcherPaths(const QStringList& paths)
{
    return m_fileWatcher.addPaths(paths);
}

void CMyFileSystem::removeWatcherPath(const QString& path)
{
    m_fileWatcher.removePath(path);
}

void CMyFileSystem::removeWatcherPaths(const QStringList& paths)
{
    if (paths.empty()) {
        return;
    }
    m_fileWatcher.removePaths(paths);
}

QStringList CMyFileSystem::GetFilesWather()
{
    return m_fileWatcher.files();
}

QStringList CMyFileSystem::GetDirectoriesWather()
{
    return m_fileWatcher.directories();
}

void CMyFileSystem::slotDirectoryChanged(const QString& path)
{
    m_strDirectorPath = path;

    if (!m_directorTimer.isActive()) {
        m_directorTimer.start(200);
    }
}

void CMyFileSystem::slotFileChanged(const QString& path)
{
    m_strFilePath = path;

    if (!m_fileTimer.isActive()) {
        m_fileTimer.start(200);
    }
}

void CMyFileSystem::slotFileTimerOut()
{
    emit signalFileChanged(m_strFilePath);
}

void CMyFileSystem::slotDirectorTimerOut()
{
    emit signalDirectoryChanged(m_strDirectorPath);
}
